package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.Orders;
import com.zr.model.User;
import com.zr.service.OrderService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@Scope(value = "prototype")
public class OrderAction extends ActionSupport implements ServletResponseAware, SessionAware, ServletRequestAware {

	private HttpServletRequest req; // HttpServletRequest型变量request声明
	private HttpServletResponse resp; // HttpServletResponse型变量response声明
	private Map sion; // HttpSession型变量session声明

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.resp = response;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.sion = session;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}

	
	@Resource
	private OrderService orderService;

	// 返回所有配送点
	public void getAllpoint() {
		resp.setCharacterEncoding("UTF-8");
		JSONArray js = orderService.getAllPoint();
		PrintWriter pw;
		try {
			pw = resp.getWriter();
			pw.write(js.toString());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 返回所有代理点
	public void getAllProxy() {
		resp.setCharacterEncoding("UTF-8");
		JSONArray js = orderService.getAllProxy();
		PrintWriter pw;
		try {
			pw = resp.getWriter();
			pw.write(js.toString());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// 返回所有订单状态
		public void getAllOstatus() {
			resp.setCharacterEncoding("UTF-8");
			JSONArray js = orderService.getAllOstatus();
			PrintWriter pw;
			try {
				pw = resp.getWriter();
				pw.write(js.toString());
				pw.flush();
				pw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	// 返回所有订单类别
	public void getAllOType() {
		resp.setCharacterEncoding("UTF-8");
		JSONArray js = orderService.getAllType();
		PrintWriter pw;
		try {
			pw = resp.getWriter();
			pw.write(js.toString());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 返回所有物品类别
	public void getAllGoodsType() {
		resp.setCharacterEncoding("UTF-8");
		JSONArray js = orderService.getAllGoodsType();
		PrintWriter pw;
		try {
			pw = resp.getWriter();
			pw.write(js.toString());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 录入新订单
	private Orders order;

	public Orders getOrder() {
		return order;
	}

	public void setOrder(Orders order) {
		this.order = order;
	}

	public void addOrder() {
		resp.setCharacterEncoding("UTF-8");
		User u = (User) sion.get("user");
		if (orderService.addOrder(order, u)) {
			try {
				PrintWriter pw = resp.getWriter();
				pw.write("1");
				pw.flush();
				pw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	// 返回用户的所有订单
	private Integer page;
	private Integer rows;
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getRows() {
		return rows;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	public void findYHOrder() {
		resp.setCharacterEncoding("UTF-8");
		User u = (User) sion.get("user");
		JSONObject js = orderService.findYHOrder(u, page, rows);
		PrintWriter pw;
		try {
			pw = resp.getWriter();
			pw.write(js.toString());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//用户撤销订单
	private Integer oid;
	private String ostatus;
	public Integer getOid() {
		return oid;
	}
	public void setOid(Integer oid) {
		this.oid = oid;
	}
	public String getOstatus() {
		return ostatus;
	}
	public void setOstatus(String ostatus) {
		this.ostatus = ostatus;
	}
	public void cxOrder(){
		resp.setCharacterEncoding("UTF-8");
		if (orderService.removeOrder(oid,ostatus)) {
			try {
				PrintWriter pw = resp.getWriter();
				pw.write("1");
				pw.flush();
				pw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//用户取消订单
	private String qxorder;
		public String getQxorder() {
		return qxorder;
	}
	public void setQxorder(String qxorder) {
		this.qxorder = qxorder;
	}
	public void updateAndAddorder(){
		resp.setCharacterEncoding("UTF-8");
		JSONObject json = JSONObject.fromObject(qxorder);
		Orders os = (Orders) JSONObject.toBean(json,Orders.class);
  		int i =orderService.updateAndAddorder(os);	
			try {
				PrintWriter pw = resp.getWriter();
				pw.write(i+"");
				pw.flush();
				pw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}

		
		}
	//返回所有订单
	public void findAllOrder() {
		resp.setCharacterEncoding("UTF-8");
		JSONObject js = orderService.findAllOrder(page, rows);
		PrintWriter pw;
		try {
			pw = resp.getWriter();
			pw.write(js.toString());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	//条件查找订单
	private Orders order1;
	public Orders getOrder1() {
		return order1;
	}
	public void setOrder1(Orders order1) {
		this.order1 = order1;
	}
	private String strfhdate;
	private String overfhdate;	
	public String getStrfhdate() {
		return strfhdate;
	}
	public void setStrfhdate(String strfhdate) {
		this.strfhdate = strfhdate;
	}
	public String getOverfhdate() {
		return overfhdate;
	}
	public void setOverfhdate(String overfhdate) {
		this.overfhdate = overfhdate;
	}

	public void findOrder() {
		resp.setCharacterEncoding("UTF-8");
		if("".equals(order1.getPlace())){
			order1.setPlace("1");
		}
		if("".equals(order1.getReceipt())){
			order1.setReceipt("1");
		}
		if("".equals(order1.getOrderstatus())){
			order1.setOrderstatus("1");
		}
		if("".equals(order1.getType())){
			order1.setType("1");
		}
		if("".equals(order1.getPaystate())){
			order1.setPaystate("1");
		}	
		if("".equals(strfhdate)){
			strfhdate="1";
		}
		if("".equals(overfhdate)){
			overfhdate="1";
		}
		JSONArray js = orderService.findOrder(order1,strfhdate,overfhdate);
		System.out.println(js.toString());
		PrintWriter pw;
		try {
			pw = resp.getWriter();
			pw.write(js.toString());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//修改订单支付状态
	private Orders order2;
	public Orders getOrder2() {
		return order2;
	}
	public void setOrder2(Orders order2) {
		this.order2 = order2;
	}
	public void updateOrderState(){
		resp.setCharacterEncoding("UTF-8");
		if (orderService.updateOrderState(order2)){	
			try {
				PrintWriter pw = resp.getWriter();
				pw.write("1");
				pw.flush();
				pw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
	//返回配送点的所有订单
	public void findPointOrder() {
		resp.setCharacterEncoding("UTF-8");
		User u = (User) sion.get("user");
		JSONObject js = orderService.findPointOrder(u, page, rows);
		PrintWriter pw;
		try {
			pw = resp.getWriter();
			pw.write(js.toString());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//获取价格
	public void getjiage() {
		resp.setCharacterEncoding("UTF-8");
		JSONObject js =orderService.getjiage(order);
		PrintWriter pw;
		try {
			pw = resp.getWriter();
			pw.write(js.toString());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
